package kitwee.com.kanpad.bean;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import kitwee.com.kanpad.R;

/**
 * Created by zhouwei on 16/11/30.
 */

public class ReasonAdapter extends RecyclerView.Adapter{
    private Context context;
    private List<ListDetailBean.PauseStatusRemarksBean> mData;
//
    public ReasonAdapter(Context context, List<ListDetailBean.PauseStatusRemarksBean> mData) {
        this.context = context;
        this.mData = mData;
    }


    public void setData(List<ListDetailBean.PauseStatusRemarksBean> data) {
        mData = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.pause_reason_item,null));
    }

    private int layoutPosition;
    private boolean select;
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ViewHolder){
            final ViewHolder viewHolder = (ViewHolder) holder;

            viewHolder.mTextView.setText(mData.get(position).getDict_value());
            ((ViewHolder)holder).itemView.setTag(position);
            if(mData.size()>0){
                if (mOnItemClickLitener != null) {
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            select = true;
                            layoutPosition = viewHolder.getLayoutPosition();
                            notifyDataSetChanged();
                            mOnItemClickLitener.onItemClick(viewHolder.itemView, (int) holder.itemView.getTag());
                        }
                    });

                    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            int pos = viewHolder.getLayoutPosition();
                            mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                            return false;
                        }
                    });
                }
            }

            if (position == layoutPosition && select) {
                ((ViewHolder)holder).itemView.setBackgroundColor(context.getResources().getColor(R.color.itemclick));
            } else {
                ((ViewHolder)holder).itemView.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
        }


    }

    @Override
    public int getItemCount() {
        return mData == null ? 0:mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView mTextView;
        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.text);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private BomvoListAdapter.OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(BomvoListAdapter.OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
